var  baseUrl = 'http://joqiuyf.etjbooks.com.cn:8021/';
// var  baseUrl = 'https://activity.etjbooks.com.cn/';

function ajax(requestUrl,method,requestData,SuccessCallback){
    var str = '';
    if(Object.keys(requestData).length !== 0) {
        str = JSON.stringify(requestData);
    }
    // console.log(str)
    // loading = layer.load(5);
    $.ajax({
        type: method,
        url: baseUrl + requestUrl,
        data: requestData,
        contentType: "application/json",
        dataType: "json",
        sync: false,
        success: function(data) {
            var obj = null;
            try {
                obj = eval('(' + data + ')');
            } catch (ex) {
                obj = data;
            }
            if(obj.status == 200) {
                SuccessCallback(obj.data);
            }
        },
        error: function(err) {
            // layer.msg(err.responseJSON.msg);
        },
        complete: function(XHR, TS) {
            XHR = null
        }
    });
}

function splitDigits(array, groupSize = 6) {
    // const digits = Math.abs(num).toString().split('').map(Number);
    const groups = [];
    let index = 0;
    while (index < array.length)  {
        groups.push(array.slice(index,  index += groupSize));
    }
    return groups;
}


function  getCover(array) {
    var url = ''
    var cover = '';
    var type;
    var file_vframe
    // console.log(array)
    $.each(array,function (i,v) {
        if(v.ext == "jpg" || v.ext == "png") {
            cover = v.file_url;
            file_vframe = v.file_vframe
            return false;
        }
        if(v.ext == "mp4") {
            cover = v.file_vframe;
            file_vframe = v.file_vframe
            return false;
        }
    })
    return {
        url:file_vframe,
        cover:cover
    };
}


$(function (){
    var mySwiper = new Swiper('.swiper-container',{
        loop:true,
        speed:1000,
        navigation: {
            nextEl: '.swiper-next',
            prevEl: '.swiper-prev',
        },
        // autoplay: {
        //     delay: 5000,//1秒切换一次
        //     disableOnInteraction: false,
        // }
    })
    var obj = {
        topic:1,
        page_count:54,
        page:1
    }

    var objHc =  {
        1:[],
        2:[],
        3:[],
        4:[],
        5:[],
        6:[],
        7:[]
    }

    addSwiper();


    $(".l-r").click(function (){
        console.log(objHc);
        $(".l-r").removeClass('active');
        $(this).addClass('active');
        var num = $(this).attr('data-type');
        obj.topic = num;
        obj.page = 1;

        mySwiper.removeAllSlides();
        if(objHc[obj.topic].length > 0) {
            renderDom(objHc[obj.topic])
        } else {
            addSwiper();
        }
    })


    $("body").on('click','.item-i',function (){
        var id = $(this).attr('data-id');
        ajax('ajax_article/'+ id,'get',{},function (res){
            var data = res[0];
            console.log(data)
            if(data.ext == 'mp4') {
                $("#dialogVideo").attr('src',data.file_url)
                $("#vidoeDialog").removeClass('hide');
                document.getElementById('dialogVideo').play();
                mySwiper.autoplay.stop();
            } else {
                $("#imgDialog .inner").css("background-image","url(" + data.file_url + ")")
                $("#imgDialog").removeClass('hide');
                mySwiper.autoplay.stop();
            }
        })
        return false;
    })

    $(".mark").click(function (){
        $(".dialog").addClass('hide');
        document.getElementById('dialogVideo').pause();
    })




    function addSwiper() {
        // console.log(obj)
        ajax('ajax_article_resource','get',obj,function (res){
            var html = '';
            var data = res.data;
            var array = splitDigits(data,9);
            //console.log(array);
            objHc[obj.topic] = array;
            renderDom(array);
            obj.page ++ ;
            if( res.current_page >= res.last_page) {
            } else {
                // addSwiper();
            }
        })
    }
    function  renderDom (array) {
        var html = '';
        $.each(array,function (i,v) {
            html += '<div class="swiper-slide type2">';
            $.each(v,function (x,y) {
                var imgurl = getCover(y.file_list);
                //console.log(imgurl)
                html += ' <a class="item item-i imgBox" data-id="'+ y.id +'"  style="background-image: url('+ imgurl.cover +')">\n' +
                    '                      <div class="bottomTtitle"><span>'+ y.id +'*</span>'+ y.id + + y.title + '</div>' +
                    '                  </a>'
            })
            html += '</div>';
        })

        mySwiper.appendSlide(html);
        mySwiper.slideTo(1, 0, false)
    }
})
